package com.cat.dao.Impl;

import com.cat.execption.DaoExecption;
import com.cat.pojo.Student;
import com.cat.util.JdbcUtils;
import com.cat.dao.inter.StudentDao;

import java.sql.*;
import java.util.*;

/**
 * @author Misterchaos
 */
public class StudentDaoImpl extends AbstractDao implements StudentDao {

    private static Connection conn = null;
    private static PreparedStatement ps = null;

    public StudentDaoImpl() {
    }


    @Override
    public void addStudent(Object obj) throws DaoExecption {
        String sql = "insert into tb_student (student_id,student_name,phone_number,password,user_type,portrait" +
                ",gender,major,major_class,position,introduction) " +
                "values (?,?,?,?,?,?,  ?,?,?,?,?)";

        Student student = (Student) obj;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement(sql);
            mapping2Sql(ps, student);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new DaoExecption("failed to add student into database", e);
        }
        JdbcUtils.free(null, ps, conn);
    }

    @Override
    public List<Student> findStudent(String attr, String value) {
        String sql = "select student_id as StudentId ,student_name as StudentName," +
                "phone_number as PhoneNumber,password as PassWord,user_type as UserType ,portrait as Portrait," +
                "gender as Gender ,major as Major,major_class as MajorClass ,position as Position, introduction as Introduction from tb_student ";
        if ("studentId".equalsIgnoreCase(attr)) {
            sql += "where student_id = \'" + value + "\'";
        } else if ("studentName".equalsIgnoreCase(attr)) {
            sql += "where student_name = \'" + value + "\'";
        } else if ("gender".equalsIgnoreCase(attr)) {
            sql += "where gender =  \'" + value + "\'";
        } else if ("major".equalsIgnoreCase(attr)) {
            sql += "where major =  \'" + value + "\'";
        } else if ("position".equalsIgnoreCase(attr)) {
            sql += "where position = \'" + value + "\'";
        }
        System.out.println("find student by ：" + attr + " value = " + value);
        Student stu = null;
        List list = getResultList(sql, Student.class);
        List stuList = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            stuList.add((Student) list.get(i));
        }
        return stuList;
    }


    @Override
    public List<Object> getAllStudent() {

        List<Object> list = getResultList("select student_id as StudentId ,student_name as StudentName," +
                "phone_number as PhoneNumber,password as PassWord,user_type as UserType ,portrait as Portrait," +
                "gender as Gender ,major as Major,major_class as MajorClass ,position as Position, introduction as Introduction from tb_student", Student.class);
        return list;
    }


    @Override
    public void updateStudent(Student stu) {
        String sql = "update tb_student set student_id = ? ,student_name = ?,phone_number = ?,password = ?,user_type = ? ,portrait = ?" +
                ",gender = ?,major = ?,major_class = ?,position = ? , introduction = ? where student_id = ? ";

        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement(sql);
            mapping2Sql(ps, stu);
            ps.setString(12, stu.getStudentId());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new DaoExecption("failed to update portrait to " + stu.getPortrait() + "where id = " + stu.getStudentId(), e);
        }
        JdbcUtils.free(null, ps, conn);
    }


    @Override
    public void deleteStudent(Object obj) {
        String sql = "delete from tb_student where student_id = ?";
        Student stu = (Student) obj;
        try {
            conn = JdbcUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, stu.getStudentId());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new DaoExecption(e.getMessage(), e);
        }
        JdbcUtils.free(null, ps, conn);
    }


    /**
     * @param stu
     * @Description: mapping a student's information to a sql query
     * @Param: ps
     * @return: void
     * @Author: HYC email:kobe524348@gmail.com
     * @Date: 2019/3/26
     */
    private void mapping2Sql(PreparedStatement ps, Student stu) throws SQLException {
        ps.setString(1, stu.getStudentId());
        ps.setString(2, stu.getStudentName());
        ps.setString(3, stu.getPhoneNumber());
        ps.setString(4, stu.getPassword());
        ps.setString(5, stu.getUserType());
        ps.setString(6, stu.getPortrait());
        ps.setString(7, stu.getGender());
        ps.setString(8, stu.getMajor());
        ps.setString(9, stu.getMajorClass());
        ps.setString(10, stu.getPosition());
        ps.setString(11, stu.getIntroduction());
    }
}
